const {
  toUrl,
  globalData
} = getApp();
import {
  request
} from "@/utils/request";
Page({
  toUrl,
  data: {
    page: 1,
    count: 1,
    isExit: false,
    title: "精选商品",
    list: [],
    globalData,
  },
  //获取精选商品列表
  getGoodsList() {
    if (this.data.page > this.data.count) {
      this.setData({
        isExit: true,
      });
      return;
    }
    const that = this;
    const data = {
      page: this.data.page,
    };
    request("index/goodslist", {
      data
    }).then((res) => {
      let list2 = that.data.list
      res.data.map((it)=>{
        list2.push(it)
      })
      that.setData({
          list: list2,
          page: ++that.data.page,
          count: res.count,
      });
    });
  },
  //获取热兑商品列表
  getList() {
    if (this.data.page > this.data.count) {
      this.setData({
        isExit: true,
      });
      return;
    }
    const that = this;
    const data = {
      is_recommend: 1,
      page: this.data.page,
    };
    request("user/integrallist", {
      data
    }).then((res) => {
      that.setData({
        list: that.data.list.concat(res.data),
        page: ++that.data.page,
        count: res.count,
      });
    });
  },
  //跳转//pages/me/score/detail
  goGoodsDetail(e) {
    const id = e.currentTarget.dataset.id;
    if (this.data.title == "热兑商品") {
      wx.navigateTo({
        url: "/pages/me/score/detail?id=" + id,
      });
    } else {
      wx.navigateTo({
        url: "/pages/store/goods/detail?id=" + id,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      title
    } = options;
    this.setData({
      title: title,
    });
    if (this.data.title == "热兑商品") {
      this.getList();
    } else {
      this.getGoodsList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
    if (this.data.isExit) {
      return;
    } else {
      if (this.data.title == "精选商品") {
        this.getGoodsList();
      } else if (this.data.title == "热兑商品") {
        this.getList();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});